package com.ruoyi.common.flowable;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.mapper.ActHiProcinstMapper;
import com.ruoyi.service.FlowInstanceExtServiceImpl;
import com.ruoyi.service.SysUserServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * 任务节点前置监听处理类
 * @author: Lu Yang
 * @create: 2019-05-04 20:51
 **/
@Component
public class ProcessCreatedListener implements FlowableEventListener {

    public FlowInstanceExtServiceImpl getFlowInstanceExtServiceImpl() {
        return SpringUtils.getBean(FlowInstanceExtServiceImpl.class);
    }

    public SysUserServiceImpl getSysUserService() {
        return SpringUtils.getBean(SysUserServiceImpl.class);
    }

    public TaskService getTaskService() {
        return SpringUtils.getBean(TaskService.class);
    }

    public RepositoryService getRepositoryService() {
        return SpringUtils.getBean(RepositoryService.class);
    }

    public HistoryService getHistoryService() {
        return SpringUtils.getBean(HistoryService.class);
    }

    private List<String> getSponsorDirector(Long deptId){
        List<String> users = null;//getSysUserService().getBaseMapper().getSponsorDirector(deptId);
        return users;
    }

    public static ActHiProcinstMapper getActHiProcinstMapper() {
        return SpringUtils.getBean(ActHiProcinstMapper.class);
    }

    private List<String> getSponsorColleague(Long deptId){
        List<String> users = null;//getSysUserService().getBaseMapper().getSponsorDirector(deptId);
        return users;
    }

    public static RuntimeService getRuntimeService() {
        return SpringUtils.getBean(RuntimeService.class);
    }

    @Override
    public void onEvent(FlowableEvent event) {
        // 当前节点任务实体
        ExecutionEntityImpl taskEntity = (ExecutionEntityImpl) ((FlowableEntityEventImpl) event).getEntity();
        getFlowInstanceExtServiceImpl().updateExt(taskEntity.getProcessInstanceId());
        System.out.println("1111111111");
        List<Process> processes = getRepositoryService().getBpmnModel(taskEntity.getProcessDefinitionId()).getProcesses();
        for (Process process : processes) {
            Collection<FlowElement> flowElements = process.getFlowElements();
            if (CollectionUtils.isEmpty(flowElements)) {
                continue;
            }
            for (FlowElement flowElement : flowElements) {
                if (!(flowElement instanceof UserTask)) {
                    continue;
                }
                UserTask userTask = (UserTask) flowElement;
                /*String userType = (userTask.getAttributes().get("userType").get(0)).getValue();
                if("jointlySign".equals(userType)){
                    String multiInstanceAssignee = userTask.getAttributes().get("multiInstanceAssignee").get(0).getValue();
                    List<String> userList = TaskCreatedListener.usersStrToList(multiInstanceAssignee);

                    MultiInstanceLoopCharacteristics loopCharacteristics = userTask.getLoopCharacteristics();
                    String inputDataItem = loopCharacteristics.getInputDataItem();
                    Map vars = new HashMap();
                    vars.put(inputDataItem,userList);
                    getRuntimeService().setVariables(taskEntity.getId(), vars);
                }*/
            }
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}